<template>
  <div class="app-container">
    <template>
      <div class="hidden-md-and-up">
        <el-alert title="如若不适应手机端操作，建议使用电脑操作" type="warning" />
      </div>
    </template>
    <div class="table-container">
      <div style="padding-bottom:20px; display: flex;">
        <div style="width: 200px;">
          <el-input v-model="search" size="medium" placeholder="输入关键字搜索" />
        </div>
        <div style="margin-left: 20px;" />
        <el-button type="success" size="medium" plain icon="el-icon-download" @click="excel_dialog = true">导出</el-button>
      </div>
      <el-table
        v-loading="paperListLoading"
        :data="paperList"
        element-loading-text="Loading"
        border
        fit
      >
        <el-table-column type="selection" align="center" width="55" />
        <!-- <el-table-column align="center" label="序号" width="55">
          <template slot-scope="scope">
            {{ scope.$index+1 }}
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="成果名称" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.achievementName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="所属机构" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.organization }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="鉴定部门" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.identifyDept }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="鉴定日期" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.identifyDate }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="鉴定结论" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.identifyResult }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="鉴定号" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.identifyNum }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="学校署名" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.schoolSigned }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="所有作者" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.allAuthor }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="审批结果" min-width="100">
          <template slot-scope="scope">
            {{ scope.row.identifyResult }}
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          label="状态"
          :filters="[{ text: '草稿', value: '草稿' }, { text: '待院级审核', value: '待院级审核' }, { text: '被院级退回', value: '被院级退回' }, { text: '待校级审核', value: '待校级审核' }, { text: '被校级退回', value: '被校级退回' }, { text: '审核通过', value: '审核通过' }]"
          :filter-method="filterTag"
          filter-placement="bottom-end"
          min-width="120"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.status | statusFilter">{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="230">
          <template slot-scope="scope">
            <el-popover
              :ref="`popoverpass-${scope.$index}`"
              placement="top"
              width="160"
            >
              <p>确认通过吗？该操作不可撤销</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="scope._self.$refs[`popoverpass-${scope.$index}`].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="thesis_pass(scope.$index, scope.row)">确定</el-button>
              </div>
            </el-popover>
            <el-button
              size="mini"
              @click="open_infoview_dialog(scope.row)"
            >
              查看
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="thesis_back(scope.row)"
            >
              退回
            </el-button>
            <el-button
              v-popover="`popoverpass-${scope.$index}`"
              size="mini"
              type="success"
            >
              通过
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :current-page="p1_page"
          :page-sizes="[1,2,3,10, 20, 30, 40]"
          :page-size="p1_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="p1_total"
          @size-change="p1_sizechange"
          @current-change="p1_currentchange"
        />
        <!-- @size-change="handleSizeChange"
          @current-change="handleCurrentChange" -->
        <!-- 使用了size-change和current-change事件来处理页码大小和当前页变动时候触发的事件。 -->
      </div>
      <el-dialog title="信息总览" :visible.sync="infoview_dialog" width="75%">
        <el-descriptions title="专利信息" :column="2" size="medium" border :label-style="{width:'25%'}" :content-style="{width:'25%'}">
          <el-descriptions-item label="成果鉴定ID">
            {{ infoview1.identifyID }}
          </el-descriptions-item>
          <el-descriptions-item label="录入人">
            {{ infoview1.entered }}
          </el-descriptions-item>
          <el-descriptions-item label="统计分类">
            {{ infoview1.classifi }}
          </el-descriptions-item>
          <el-descriptions-item label="所属机构">
            {{ infoview1.organization }}
          </el-descriptions-item>
          <el-descriptions-item label="成果名称">
            {{ infoview1.achievementName }}
          </el-descriptions-item>
          <el-descriptions-item label="鉴定部门">
            {{ infoview1.identifyDept }}
          </el-descriptions-item>
          <el-descriptions-item label="鉴定级别">
            {{ infoview1.identifyLevel }}
          </el-descriptions-item>
          <el-descriptions-item label="鉴定结论">
            {{ infoview1.identifyResult }}
          </el-descriptions-item>
          <el-descriptions-item label="所有作者">
            {{ infoview1.allAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="鉴定日期">
            {{ infoview1.identifyDate }}
          </el-descriptions-item>
          <el-descriptions-item label="院系">
            {{ infoview1.dept }}
          </el-descriptions-item>
          <el-descriptions-item label="科技成果登记证书号">
            {{ infoview1.certificateID }}
          </el-descriptions-item>
          <el-descriptions-item label="完成形式">
            {{ infoview1.completionForm }}
          </el-descriptions-item>
          <el-descriptions-item label="通讯作者">
            {{ infoview1.correspondingAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="学科门类">
            {{ infoview1.discipline }}
          </el-descriptions-item>
          <el-descriptions-item label="鉴定号">
            {{ infoview1.identifyNum }}
          </el-descriptions-item>
          <el-descriptions-item label="学校署名">
            {{ infoview1.schoolSigned }}
          </el-descriptions-item>vv
          <el-descriptions-item label="第一作者">
            {{ infoview1.leadAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第二作者">
            {{ infoview1.secondAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第三作者">
            {{ infoview1.thirdAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第四作者">
            {{ infoview1.fourthAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第五作者">
            {{ infoview1.fifthAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第六作者">
            {{ infoview1.sixthAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第七作者">
            {{ infoview1.seventhAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第八作者">
            {{ infoview1.eighthAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="第九作者">
            {{ infoview1.ninthAuthor }}
          </el-descriptions-item>
          <el-descriptions-item label="审核状态">
            <el-tag :type="infoview1.status | statusFilter">{{ infoview1.status }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="未通过原因">
            {{ infoview1.reason }}
          </el-descriptions-item>
          <el-descriptions-item label="备注">
            {{ infoview1.notes }}
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions class="margin-top" title="作者信息" :column="1" size="medium" direction="vertical" :colon="false">
          <el-descriptions-item>
            <el-table
              v-loading="paperAuthorListLoading"
              :data="paperAuthorList"
              element-loading-text="Loading"
              border
              fit
            >
              <el-table-column align="center" label="姓名" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.name }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="所属院系" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.dept }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="排名" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.ranking }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="作者类型" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.authorCate }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="考核系数" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.assessment }}
                </template>
              </el-table-column>
            </el-table>
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions class="margin-top" title="关联项目信息" :column="1" size="medium" direction="vertical" :colon="false">
          <el-descriptions-item>
            <el-table
              v-loading="paperProjectListLoading"
              :data="paperProjectList"
              element-loading-text="Loading"
              border
              fit
            >
              <el-table-column align="center" label="项目名称" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.projectName }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="项目负责人" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.head }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="署名单位" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.signedUnit }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="开始日期" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.startTime }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="结束日期" min-width="100">
                <template slot-scope="scope">
                  {{ scope.row.endTime }}
                </template>
              </el-table-column>
            </el-table>
          </el-descriptions-item>
        </el-descriptions>
        <el-descriptions class="margin-top" title="附件信息" :column="1" size="medium" direction="vertical" border>
          <el-descriptions-item label="附件一">
            {{ infoview1.annex }}
          </el-descriptions-item>
        </el-descriptions>
        <div slot="footer" class="dialog-footer">
          <el-button @click="infoview_dialog = false">关 闭</el-button>
        </div>
      </el-dialog>
      <el-dialog title="导出excel" :visible.sync="excel_dialog" width="60%">
        <el-alert
          title="请选择需要导出的数据"
          type="success"
          description="选择的数据为表头，未选择的数据将不会导出"
          :closable="false"
        />
        <div style="margin: 15px;">
          <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选</el-checkbox>
          <div style="margin: 15px 0;" />
          <el-checkbox-group v-model="check_fields" @change="handleCheckedFieldsChange">
            <el-checkbox v-for="field in fields_list" :key="field" :label="field">{{ field }}</el-checkbox>
          </el-checkbox-group>
        </div>
        <el-alert
          title="请选择时间范围"
          type="success"
          description="选择导出数据的时间范围"
          :closable="false"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="excel_dialog = false">关 闭</el-button>
          <download-excel
            style="display:inline-block !important;margin-left:10px;"
            :data="EXCEL.DetailsForm"
            :fields="EXCEL.json_fields"
            :header="EXCEL.title"
            type="xls"
            name="论文数据导出.xls"
            :fetch="createExportData"
            :before-generate="startDownload"
            :before-finish="finishDownload"
          >
            <!-- 上面可以自定义自己的样式，还可以引用其他组件button -->
            <el-button type="success" plain>确认导出</el-button>
          </download-excel>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { get_thesis_xiaoji, thesis_pass_xiaoji, thesis_back_xiaoji, get_thesis_author, get_thesis_project, get_file } from '@/api/identify'
import { Message, Loading } from 'element-ui'
import { mapGetters } from 'vuex'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        草稿: 'info',
        待院级审核: '',
        被院级退回: 'warning',
        待校级审核: '',
        被校级退回: 'danger',
        审核通过: 'success'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      search: '',
      infoview_dialog: false,
      paperList: null,
      paperListLoading: false,
      paperAuthorList: null,
      paperAuthorListLoading: false,
      paperProjectList: null,
      paperProjectListLoading: false,
      p1_size: 10,
      p1_total: 0,
      p1_page: 1,
      back_post: {
        id: '',
        reason: ''
      },
      infoview1: {
        achievementName: '',
        allAuthor: '',
        annex: '',
        associatedProjects: '',
        certificateID: '',
        classifi: '',
        completionForm: '',
        correspondingAuthor: '',
        dept: '',
        discipline: '',
        eighthAuthor: '',
        entered: '',
        enteredDate: '',
        fifthAuthor: '',
        fourthAuthor: '',
        identifyDate: '',
        identifyDept: '',
        identifyID: '',
        identifyLevel: '',
        identifyNum: '',
        identifyResult: '',
        leadAuthor: '',
        ninthAuthor: '',
        notes: '',
        organization: '',
        reason: '',
        schoolSigned: '',
        secondAuthor: '',
        seventhAuthor: '',
        sixthAuthor: '',
        status: '',
        thirdAuthor: ''
      },
      excel_dialog: false,
      all_fields: {
        '成果名称': 'achievementName',
        '所有作者': 'allAuthor',
        '关联项目': 'associatedProjects',
        '科技成果登记证书号': 'certificateID',
        '统计分类': 'classifi',
        '完成形式': 'completionForm',
        '通讯作者': 'correspondingAuthor',
        '院系': 'dept',
        '学科门类': 'discipline',
        '第八作者': 'eighthAuthor',
        '录入人': 'entered',
        '录入日期': 'enteredDate',
        '第五作者': 'fifthAuthor',
        '第四作者': 'fourthAuthor',
        '鉴定日期': 'identifyDate',
        '鉴定部门': 'identifyDept',
        '成果鉴定ID': 'identifyID',
        '鉴定级别': 'identifyLevel',
        '鉴定号': 'identifyNum',
        '鉴定结论': 'identifyResult',
        '第一作者': 'leadAuthor',
        '第九作者': 'ninthAuthor',
        '备注': 'notes',
        '所属机构': 'organization',
        '未通过原因': 'reason',
        '学校署名': 'schoolSigned',
        '第二作者': 'secondAuthor',
        '第七作者': 'seventhAuthor',
        '第六作者': 'sixthAuthor',
        '审核阶段': 'status',
        '第三作者': 'thirdAuthor'
      },
      fields_list: [
        'achievementName',
        'allAuthor',
        'associatedProjects',
        'certificateID',
        'classifi',
        'completionForm',
        'correspondingAuthor',
        'dept',
        'discipline',
        'eighthAuthor',
        'entered',
        'enteredDate',
        'fifthAuthor',
        'fourthAuthor',
        'identifyDate',
        'identifyDept',
        'identifyID',
        'identifyLevel',
        'identifyNum',
        'identifyResult',
        'leadAuthor',
        'ninthAuthor',
        'notes',
        'organization',
        'reason',
        'schoolSigned',
        'secondAuthor',
        'seventhAuthor',
        'sixthAuthor',
        'status',
        'thirdAuthor'
      ],
      check_fields: [],
      checkAll: false,
      isIndeterminate: false,
      EXCEL: {
        title: '测试表格',
        json_fields: null,
        DetailsForm: null
      },
      fileList: [],
      fileListLoading: false,
      fileForm: {
        patentid: '',
        filedate: '',
        fileid: '',
        filename: '',
        filesize: '',
        fileurl: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'uid',
      'roles',
      'name'
    ]),
    XiaojiPaperPost() {
      return {
        name: this.name,
        pageNum: this.p1_page,
        pageSize: this.p1_size
      }
    }
  },
  created() {
    this.fetchPaperData()
  },
  methods: {
    fetchPaperData() {
      this.paperListLoading = true
      get_thesis_xiaoji(this.XiaojiPaperPost).then(response => {
        if (response.code === 200) {
          this.paperList = response.data.list
          this.p1_total = response.data.total
          this.paperListLoading = false
        }
      })
    },
    p1_sizechange(newsize) {
      this.p1_size = newsize
      this.p1_page = 1
      this.fetchPaperData()
    },
    p1_currentchange(newpage) {
      this.p1_page = newpage
      this.fetchPaperData()
    },
    filterTag(value, row) {
      return row.status === value
    },
    thesis_back(row) {
      this.$prompt('请输入退回原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^.+$/,
        inputErrorMessage: '原因不能为空'
      }).then(({ value }) => {
        this.back_post.id = row.identifyID
        this.back_post.reason = value
        thesis_back_xiaoji(this.back_post).then(response => {
          if (response.code === 200) {
            Message.success('退回成功')
            this.fetchPaperData()
          }
        })
      }).catch(() => {
        Message.info('取消退回')
      })
    },
    thesis_pass(index, row) {
      this.$refs[`popoverpass-${index}`].doClose()
      thesis_pass_xiaoji(row.identifyID).then(response => {
        if (response.code === 200) {
          Message.success('通过成功')
          this.fetchPaperData()
        }
      })
    },
    open_infoview_dialog(row) {
      this.infoview_dialog = true
      this.infoview1 = JSON.parse(JSON.stringify(row))
      this.paperAuthorListLoading = true
      get_thesis_author(row.identifyID).then(response => {
        if (response.code === 200) {
          this.paperAuthorList = response.data
          this.paperAuthorListLoading = false
        }
      })
      this.paperProjectListLoading = true
      get_thesis_project(row.identifyID).then(response => {
        if (response.code === 200) {
          this.paperProjectList = response.data
          this.paperProjectListLoading = false
        }
      })
      this.fileListLoading = true
      get_file(row.identifyID).then(response => {
        if (response.code === 200) {
          this.fileList = response.data
          this.fileListLoading = false
        }
      })
    },
    downloadFile(row) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.href = row.fileurl
      a.target = '_blank'
      a.dispatchEvent(event)
    },
    // 以下为数据导出
    handleCheckAllChange(val) {
      this.check_fields = val ? this.fields_list : []
      this.isIndeterminate = false
      this.EXCEL.json_fields = JSON.parse(JSON.stringify(this.all_fields, this.check_fields))
    },
    handleCheckedFieldsChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.fields_list.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.fields_list.length
      this.EXCEL.json_fields = JSON.parse(JSON.stringify(this.all_fields, this.check_fields))
    },
    async createExportData() {
      return await get_thesis_xiaoji(this.XiaojiPaperPost).then(response => {
        if (response.code === 200) {
          if (response.data.list.length === 0) {
            Message.warning('返回数据为空，导出失败！')
            this.loadingInstance.close()
          }
          return response.data.list
        }
      })
    },
    startDownload() {
      this.excel_dialog = false
      this.loadingInstance = Loading.service({ text: '正在导出...' })
    },
    finishDownload() {
      setTimeout(() => {
        this.loadingInstance.close()
      }, 1000)
    }
  }
}
</script>

<style lang="scss">
.el-form{
  margin-left: 40px;
  .el-input,.el-select{
    width: 300px;
  }
}
@media screen and (max-width:600px) {
  .el-dialog{
    width: 100% !important;
  }
  .el-form{
    margin-left: 25px;
  }
}
</style>
